﻿using System.Collections;
using System.Collections.Generic;

namespace VFramework{

	public enum LogLevel{
		DEBUG 	= 1 << 1,
		INFO 	= 1 << 2,
		WARN 	= 1 << 3,
		ERROR 	= 1 << 4
	}

	public class VLogger : ISingleton{


	


		private Dictionary<LogLevel,List<ILogger>> loggerConfig;
		private bool closeScreen = false;

		private VLogger(){}


		public void init(){
			loggerConfig = new Dictionary<LogLevel, List<ILogger>> ();
		}

		public void release(){
			loggerConfig.Clear ();
		}

		public bool isScreenLogClosed(){
			return closeScreen;
		}

		public void setLogger(ILogger logger, params LogLevel [] loglevels){
			for (int i = 0; i < loglevels.Length; i++) {
				if (!loggerConfig.ContainsKey (loglevels [i])) {
					loggerConfig.Add (loglevels [i], new List<ILogger> ());
				}
				loggerConfig [loglevels [i]].Add (logger);
			}

		}

		public static VLogger getInstance(){
			return Singleton<VLogger>.getInstance ();
		}


		public static void screenClose(bool close){
			Singleton<VLogger>.getInstance().closeScreen = close;
		}

		public static void debug(string msg){
			print(LogLevel.DEBUG,msg,"DEBUG");
		}

		public static void info(string msg){
			print(LogLevel.INFO,msg,"INFO");
		}

		public static void warn(string msg){
			print(LogLevel.WARN,msg,"WARN");
		}

		public static void error(string msg){
			print(LogLevel.ERROR,msg,"ERROR");
		}

		public static void print(LogLevel level, string msg, string logName){
			VLogger _instance = Singleton<VLogger>.getInstance ();
			if(!_instance.loggerConfig.ContainsKey(level)){
				new ConsoleLogger ().doLog (logName +  " no default logger!!!");
				return;
			}
				
			for (int i = 0; i < _instance.loggerConfig [level].Count; i++) {


				ILogger lg = _instance.loggerConfig [level] [i];

				if ( (_instance.closeScreen &&  lg is ScreenLogger) 
					|| (lg == null)) {
					continue;
				}
				lg.doLog (string.Format("[{0}][{1}]:{2}",Util.nowTimeymd(),logName,msg));
			}
		}

	}


}
